import {
    AfterViewInit, Component, ElementRef, EventEmitter, Input, OnChanges, OnInit, Output,
    ViewChild
} from '@angular/core';
import {DomSanitizer} from "@angular/platform-browser";

@Component({
  selector: 'app-c-audio',
  templateUrl: './c-audio.component.html',
  styleUrls: ['./c-audio.component.less']
})
export class CAudioComponent implements OnInit, OnChanges, AfterViewInit {
    @Output() dataChange = new EventEmitter<object>();
    @Output() onBlur = new EventEmitter<object>();

    @Input() canEdit;

    @Input() src;

    @ViewChild('audio') audio: ElementRef;

    @Input() control;

    init = false;

    ngOnChanges() {
        if (this.init && this.control) {
            if (this.control === 'play') {
                this.audio.nativeElement.play();
            } else if (this.control === 'pause') {
                this.audio.nativeElement.pause();
            }
        }
    }

    ngAfterViewInit() {
        this.init = true;
    }


    constructor(public sanitizer: DomSanitizer) { }

    ngOnInit() {

    }

    blur() {
        this.onBlur.emit();
    }

}
